/**
 *====================================================
 * 文件名称: FinanceApplicationServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.ContainerBulkstockHistoryEntity;
import com.yuexunit.finance.project.entity.ContainerInventoryEntity;
import com.yuexunit.finance.project.entity.ContainerInventoryHistoryEntity;
import com.yuexunit.finance.project.entity.FinanceApplicationEntity;
import com.yuexunit.finance.project.entity.FinanceBulkstockEntity;
import com.yuexunit.finance.project.entity.RecieveNoticeInventoryEntity;
import com.yuexunit.finance.project.entity.TallyingEntity;
import com.yuexunit.finance.project.entity.lexicon.BadassetsStateEnum;
import com.yuexunit.finance.project.entity.lexicon.FinanceConstractStateEnum;
import com.yuexunit.finance.project.entity.lexicon.FinanceStateEnum;
import com.yuexunit.finance.project.entity.lexicon.ProductTypeEnum;
import com.yuexunit.finance.project.entity.lexicon.SupervisionStateEnum;
import com.yuexunit.finance.support.FinanceInquireParams;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.StringUtil;

/**
 * @ClassName: FinanceApplicationServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:56:17
 */
@Implements(FinanceApplicationService.class)
public class FinanceApplicationServiceImpl implements FinanceApplicationService {

	@Override
	public Long createFinanceApplication(FinanceApplicationEntity entity) {
		entity.setApplicationDateTime(new Date());
		entity.setAbnormal(Boolean.TRUE);
		entity.setAuditInterest(BigDecimal.ZERO);
		entity.setFinanceAmount(BigDecimal.ZERO);
		entity.setCreditQuota(BigDecimal.ZERO);
		entity.setCreditQuotaConsume(BigDecimal.ZERO);
		entity.setBail(BigDecimal.ZERO);
		entity.setBailAlreadyReturn(BigDecimal.ZERO);
		entity.setTotalLoanAmount(BigDecimal.ZERO);
		entity.setTotalReceivedAmount(BigDecimal.ZERO);
		entity.setFinanceNumber(SettingConstants.PRE_NUMBER_FINANCE + RandomUtil.generateTimeNumber());
		entity.setFinanceConstractState(FinanceConstractStateEnum.UNKNOWN);
		PersistUtil.insert(entity);
		return entity.getFinanceId();
	}

	@Override
	public void updateFinanceApplication(FinanceApplicationEntity entity) {
//		FinanceApplicationEntity persist = inquireFinanceApplicationEntity(entity.getFinanceId());
//		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(entity);
	}

	@Override
	public void renovateFinanceApplication(FinanceApplicationEntity entity) {
		PersistUtil.update(entity);
	}
	
	@Override
	@Transactional
	public void deleteFinanceApplication(Long financeId) {
		PersistUtil.delete(FinanceApplicationEntity.class, financeId);
	}

	@Override
	public FinanceApplicationEntity inquireFinanceApplicationEntity(Long financeId) {
		return PersistUtil.get(FinanceApplicationEntity.class, financeId);
	}
	
	@Override
	public FinanceApplicationEntity inquireFinanceApplicationEntity(String financeNumber) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		criteria.add(Restrictions.eq("financeNumber", financeNumber));
		List<FinanceApplicationEntity> list = PersistUtil.get(criteria);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}
	
	@Override
	public List<Long> inquireRecieveNoticeInventoryEntity(Long recieveNoticeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(RecieveNoticeInventoryEntity.class);
		if (recieveNoticeId != null) {
			criteria.add(Restrictions.eq("recieveNoticeId", recieveNoticeId));
		}
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("noticeInventoryId")));
		return PersistUtil.get(criteria);
	}
	
	@Override
	public List<Long> inquireTallyingEntity(Long recieveNoticeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TallyingEntity.class);
		if (recieveNoticeId != null) {
			criteria.add(Restrictions.eq("recieveNoticeId", recieveNoticeId));
		}
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("tallyingId")));
		return PersistUtil.get(criteria);
	}
	
	@Override
	public List<Long> inquireContainerBulkstockHistoryEntity(Long noticeInventoryId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ContainerBulkstockHistoryEntity.class);
		if (noticeInventoryId != null) {
			criteria.add(Restrictions.eq("noticeInventoryId", noticeInventoryId));
		}
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("bulkstockHistoryId")));
		return PersistUtil.get(criteria);
	}
	
	@Override
	public List<Long> inquireContainerInventoryHistoryEntity(Long recieveNoticeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ContainerInventoryHistoryEntity.class);
		if (recieveNoticeId != null) {
			criteria.add(Restrictions.eq("recieveNoticeId", recieveNoticeId));
		}
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("inventoryHistoryId")));
		return PersistUtil.get(criteria);
	}
	
	@Override
	public List<Long> getFinanceListIds(Integer productType) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		if(productType !=null && productType.intValue()>0 && productType !=0){
			criteria.add(Restrictions.eq("productType", ProductTypeEnum.valueOf(productType)));
		}
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}
	
	@Override
	public List<Long> getFinanceListIds3(Integer productType,String financeNumber) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		if(productType !=null && productType !=0){
			criteria.add(Restrictions.eq("productType", ProductTypeEnum.valueOf(productType)));
		}
		if (StringUtil.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}
	
	@Override
	public List<Long> getFinanceListIds2(Integer productType,String enterpriseName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		if(productType !=null && productType !=0){
			criteria.add(Restrictions.eq("productType", ProductTypeEnum.valueOf(productType)));
		}
		if (StringUtil.isNotBlank(enterpriseName)) {
			criteria.add(Restrictions.like("enterpriseName", "%" + enterpriseName + "%"));
		}
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public PageList<FinanceApplicationEntity> inquireFinanceList(FinanceInquireParams params) {
		List<Long> financeIds = null;
		if (StringUtils.isNotBlank(params.getBulkstockName()) || params.getBulkstockInfoId() != null) {// 若有搜索货物名称
			DetachedCriteria criteria = DetachedCriteria.forClass(FinanceBulkstockEntity.class);
			if (StringUtils.isNotBlank(params.getBulkstockName())) {
				criteria.add(Restrictions.like("bulkstockName", "%" + params.getBulkstockName() + "%"));
			}
			if (params.getBulkstockInfoId() != null) {
				criteria.add(Restrictions.eq("bulkstockInfoId", params.getBulkstockInfoId()));
			}
			criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
			financeIds = PersistUtil.get(criteria);
			if (financeIds.isEmpty()) {
				return new PageList<>(params.getPage());
			}
		}
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		if (financeIds != null) {
			criteria.add(Restrictions.in("financeId", financeIds));
		}
		if (StringUtil.isNotBlank(params.getEnterpriseName())) {
			criteria.add(Restrictions.like("enterpriseName", "%" + params.getEnterpriseName() + "%"));
		}
		if (params.getEnterpriseIds() != null && params.getEnterpriseIds().length > 0) {
			criteria.add(Restrictions.in("enterpriseId", params.getEnterpriseIds()));
		}
		if (params.getFinanceIds() != null && params.getFinanceIds().length > 0) {
			criteria.add(Restrictions.in("financeId", params.getFinanceIds()));
		}
		if (params.getBeginDate() != null) {
			criteria.add(Restrictions.ge("applicationDateTime", params.getBeginDate()));
		}
		if (params.getEndDate() != null) {
			criteria.add(Restrictions.le("applicationDateTime", params.getEndDate()));
		}
		if (params.getMinAmount() != null && params.getMaxAmount() != null) {
			criteria.add(Restrictions.ge("applicationAmount", params.getMinAmount()));
		}
		if (params.getMinAmount() != null && params.getMaxAmount() != null) {
			criteria.add(Restrictions.le("applicationAmount", params.getMaxAmount()));
		}
		if (StringUtils.isNotBlank(params.getFinanceNumber())) {
			criteria.add(Restrictions.like("financeNumber", "%" + params.getFinanceNumber() + "%"));
		}
		if (params.getProductType() != null && params.getProductType() != 0) {
			criteria.add(Restrictions.eq("productType", ProductTypeEnum.valueOf(params.getProductType())));
		}
		
		if (params.getIsWarehouse() != null && params.getIsWarehouse() == Boolean.TRUE) {
			criteria.add(Restrictions.in("productType", new ProductTypeEnum[]{ProductTypeEnum.PROXY_SALE, ProductTypeEnum.PURCHASING_STORAGE}));
			if (params.getFinanceState() != null && params.getFinanceState().length > 0) {
				List<FinanceStateEnum> financeStateList = new ArrayList<>();
				for (Integer f : params.getFinanceState()) {
					if (f == null) {
						continue;
					}
					financeStateList.add(FinanceStateEnum.valueOf(f));
				}
				if (financeStateList.size() > 0) {
					criteria.add(Restrictions.or(Restrictions.in("financeState", financeStateList.toArray(new FinanceStateEnum[financeStateList.size()])), Restrictions.and(Restrictions.in("financeState", new FinanceStateEnum[]{FinanceStateEnum.INSPECTIONED}), Restrictions.eq("settlementMethod", 2))));
				}
			}
		} else {
			if (params.getFinanceState() != null && params.getFinanceState().length > 0) {
				List<FinanceStateEnum> financeStateList = new ArrayList<>();
				for (Integer f : params.getFinanceState()) {
					if (f == null) {
						continue;
					}
					financeStateList.add(FinanceStateEnum.valueOf(f));
				}
				if (financeStateList.size() > 0) {
					criteria.add(Restrictions.in("financeState", financeStateList.toArray(new FinanceStateEnum[financeStateList.size()])));
				}
			}
		}
		
		if (Boolean.TRUE == params.getHasBail()) {
			criteria.add(Restrictions.gt("bail", BigDecimal.ZERO));
		} else if (Boolean.FALSE == params.getHasBail()) {
			criteria.add(Restrictions.le("bail", BigDecimal.ZERO));
		}
		if (Boolean.TRUE == params.getIsProxy()) {
			criteria.add(Restrictions.isNotNull("proxyAccountId"));
		}
		
		if (params.getEnterpriseId() != null) {
			criteria.add(Restrictions.eq("enterpriseId", params.getEnterpriseId()));
		}
		System.out.println("Thread-id:" + Thread.currentThread().getId() + "; " + " getEnterpriseId ===>>>>> " + params.getEnterpriseId());
		criteria.addOrder(Order.desc("financeId"));
		return PersistUtil.get(criteria, params.getPage());
	}
	
	@Override
	public List<Long> countWaitAuditFinanceApplication() {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		criteria.add(Restrictions.eq("financeState", FinanceStateEnum.APPROVE));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}
	
	@Override
	public List<Long> countWaitAuditFinanceApplication1() {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
//		criteria.add(Restrictions.eq("constractState", ConstractStateEnum.CONTRACT_SUBMITED));
		criteria.add(Restrictions.eq("financeState", FinanceStateEnum.WAIT_APPROVE));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countWaitContractFinanceApplication() {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		criteria.add(Restrictions.eq("financeState", FinanceStateEnum.FIRSTCOMPLETE));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}
	
	@Override
	public List<Long> countWaitContractFinanceApplication1() {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		criteria.add(Restrictions.in("financeState", new FinanceStateEnum[]{FinanceStateEnum.WAIT_PROTOCOL, FinanceStateEnum.CUSTOMER_DOCUMENT}));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}
	
	@Override
	public List<Long> countWaitSupplementContractFinanceApplication() {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		criteria.add(Restrictions.in("financeState", new FinanceStateEnum[]{FinanceStateEnum.SUPPLEMENT_CONTRACT}));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countWaitRecieveFinanceApplication() {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceBulkstockEntity.class);
		criteria.add(Restrictions.gtProperty("quantity", "recieve"));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		List<Long> financeIds = PersistUtil.get(criteria);
		if (financeIds.isEmpty()) {
			return financeIds;
		}
		criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		if (financeIds != null && financeIds.size() > 0) {
			criteria.add(Restrictions.in("financeId", financeIds));
		}
		criteria.add(Restrictions.ge("financeState", FinanceStateEnum.CONTRACT_SIGN));
		criteria.add(Restrictions.ne("financeState", FinanceStateEnum.FINISH));
		criteria.add(Restrictions.ne("financeState", FinanceStateEnum.EVALUATE));
		criteria.add(Restrictions.ne("financeState", FinanceStateEnum.UNSUMIT));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countWaitLoadFinanceApplication() {
		DetachedCriteria criteria = DetachedCriteria.forClass(ContainerInventoryEntity.class);
		criteria.add(Restrictions.isNull("loanNoticeId"));
		criteria.add(Restrictions.eq("abnormal", Boolean.TRUE));
		criteria.add(Restrictions.eq("supervisionState", SupervisionStateEnum.ALREADY_SUPERVISE));
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countWaitTransportFinanceApplication(List<Long> financeIds) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ContainerInventoryEntity.class);
		if (financeIds != null && financeIds.size() > 0) {
			criteria.add(Restrictions.in("financeId", financeIds));
		}
		criteria.add(Restrictions.isNull("transportNoticeId"));
		criteria.add(Restrictions.eq("abnormal", Boolean.TRUE));
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		criteria.add(Restrictions.eq("supervisionState", SupervisionStateEnum.ALREADY_SUPERVISE));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countWaitBadassetsFinanceApplication() {
		DetachedCriteria criteria = DetachedCriteria.forClass(ContainerInventoryEntity.class);
		criteria.add(Restrictions.eq("abnormal", Boolean.TRUE));
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		criteria.add(Restrictions.eq("badassetsState", BadassetsStateEnum.WAIT_BADASSETS));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public PageList<FinanceApplicationEntity> inquireInProcessFinanceApplicationPageList(Long[] financeIds, Long enterpriseId, Long enterpriseId1, Set<FinanceStateEnum> financeStates, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		criteria.add(Restrictions.eq("abnormal", Boolean.TRUE));
		if (enterpriseId1 != null && enterpriseId != null) {
			criteria.add(Restrictions.or(Restrictions.and(Restrictions.eq("enterpriseId", enterpriseId1), Restrictions.eq("productType", ProductTypeEnum.PROXY_SALE)), Restrictions.eq("enterpriseId", enterpriseId)));
		}else if (enterpriseId != null) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		if (financeStates != null && financeStates.size() > 0) {
			criteria.add(Restrictions.in("financeState", financeStates));
		}
		criteria.addOrder(Order.desc("applicationDateTime"));
		if (financeIds != null && financeIds.length > 0) {
			criteria.add(Restrictions.in("financeId", financeIds));
		}
		if (null != page) {
			return PersistUtil.get(criteria, page);
		} else {
			return PersistUtil.get(criteria, PageCriteria.defaultInstance());
		}
	}
	
	@Override
	public PageList<FinanceApplicationEntity> inquireInProcessFinanceApplicationPageLists(Long[] financeIds,Long[] financeContractIds, String financeNumber, Long enterpriseId, Long enterpriseId1, Set<FinanceStateEnum> financeStates, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		criteria.add(Restrictions.eq("abnormal", Boolean.TRUE));
		if (enterpriseId1 != null && enterpriseId != null) {
			criteria.add(Restrictions.or(Restrictions.and(Restrictions.eq("enterpriseId", enterpriseId1), Restrictions.eq("productType", ProductTypeEnum.PROXY_SALE)), Restrictions.eq("enterpriseId", enterpriseId)));
		}else if (enterpriseId != null) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		if (financeStates != null && financeStates.size() > 0) {
			criteria.add(Restrictions.in("financeState", financeStates));
		}
		if(StringUtil.isNotBlank(financeNumber)){
			criteria.add(Restrictions.like("financeNumber", "%" +financeNumber+ "%"));
		}
		criteria.addOrder(Order.desc("applicationDateTime"));
		if (financeIds != null && financeIds.length > 0) {
			criteria.add(Restrictions.in("financeId", financeIds));
		}
		if (financeContractIds != null && financeContractIds.length > 0) {
			criteria.add(Restrictions.in("financeContractId", financeContractIds));
		}
		if (null != page) {
			return PersistUtil.get(criteria, page);
		} else {
			return PersistUtil.get(criteria, PageCriteria.defaultInstance());
		}
	}
	
	@Override
	public List<FinanceApplicationEntity> inquireInProcessFinanceApplicationList(Long enterpriseId, Set<FinanceStateEnum> financeStates) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		criteria.add(Restrictions.eq("abnormal", Boolean.TRUE));
		if (enterpriseId != null) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		if (financeStates != null && financeStates.size() > 0) {
			criteria.add(Restrictions.in("financeState", financeStates));
		}
		criteria.addOrder(Order.desc("applicationDateTime"));
		return PersistUtil.get(criteria);
	}

	@Override
	public PageList<FinanceApplicationEntity> inquireFinancePageList(String financeNumber, String enterpriseName,Date startDate, Date endDate, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		if (StringUtils.isNotBlank(enterpriseName)) {
			criteria.add(Restrictions.like("enterpriseName", "%" + enterpriseName + "%"));
		}
		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (startDate != null) {
			criteria.add(Restrictions.ge("applicationDateTime", startDate));
		}
		if (endDate != null) {
			criteria.add(Restrictions.le("applicationDateTime", endDate));
		}
		criteria.add(Restrictions.gt("financeState", FinanceStateEnum.CONTRACT_SIGN));
		criteria.addOrder(Order.desc("enterpriseId")).addOrder(Order.desc("applicationDateTime"));
		return PersistUtil.get(criteria, page);
	}

	@Override
	public FinanceApplicationEntity inquireEnterpriseFinance(Long enterpriseId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		criteria.addOrder(Order.desc("applicationDateTime"));
		List<FinanceApplicationEntity> list = PersistUtil.get(criteria);
		if (list.isEmpty()) {
			return null;
		} else {
			return list.get(0);
		}
	}

	@Override
	public List<Long> countEnterprise(Long[] enterpriseId, Integer tradeType) {
		if (enterpriseId.length == 0) {
			return new ArrayList<Long>();
		}
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		if (enterpriseId != null && enterpriseId.length > 0) {
			criteria.add(Restrictions.in("enterpriseId", enterpriseId));
		}
		Date b = DateUtils.now();
		Date s = DateUtils.addDateField(DateUtils.now(), Calendar.MONTH, -6);
		if (tradeType == 1) {
			
		} else if (tradeType ==2) {
			criteria.add(Restrictions.between("applicationDateTime", s, b));
		} else if (tradeType ==3) {
			criteria.add(Restrictions.le("applicationDateTime", s));
		}
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("enterpriseId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<FinanceApplicationEntity> inquireFinanceList(String financeNumber, String enterpriseName,
			Date startDate, Date endDate, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		if (StringUtils.isNotBlank(enterpriseName)) {
			criteria.add(Restrictions.like("enterpriseName", "%" + enterpriseName + "%"));
		}
		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (startDate != null) {
			criteria.add(Restrictions.ge("applicationDateTime", startDate));
		}
		if (endDate != null) {
			criteria.add(Restrictions.le("applicationDateTime", endDate));
		}
		criteria.add(Restrictions.gt("financeState", FinanceStateEnum.CONTRACT_SIGN));
		criteria.addOrder(Order.desc("enterpriseId")).addOrder(Order.desc("applicationDateTime"));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<FinanceApplicationEntity> inquireFinanceList(Long enterpriseId, Set<FinanceStateEnum> financeStates) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		if (enterpriseId != null) {
			criteria.add(Restrictions.eq("enterpriseId", enterpriseId));
		}
		if (financeStates != null && financeStates.size() > 0) {
			criteria.add(Restrictions.in("financeState", financeStates));
		}
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Date> inquireWarningTime(Long[] financeIds, String colName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		if (financeIds != null && financeIds.length > 0) {
			criteria.add(Restrictions.in("financeId", financeIds));
		}
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty(colName)));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> inquireFinanceIdByProductType(Set<ProductTypeEnum> productType) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		if (productType != null && productType.size() > 0) {
			criteria.add(Restrictions.in("productType", productType));
		}
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countOutstandingFinanceApplication() {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		criteria.add(Restrictions.neProperty("totalLoanAmount", "totalReceivedAmount"));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}
}